import 'package:flutter/material.dart';
import 'package:flutter_app_demo/pages/common_web_page.dart';
import 'package:barcode_scan/barcode_scan.dart';
import 'package:flutter_app_demo/pages/shake_page.dart';

class DiscoveryPage extends StatefulWidget {
  @override
  _DiscoveryPageState createState() => _DiscoveryPageState();
}

class _DiscoveryPageState extends State<DiscoveryPage> {
  List<Map<String, IconData>> blocks = [
    {
      "开源众包": Icons.pageview,
      "开源软件": Icons.speaker_notes_off,
      "码云推荐": Icons.screen_share,
      "代码片段": Icons.assignment,
    },
    {
      "扫一扫": Icons.camera_alt,
      "摇一摇": Icons.camera,
    },
    {
      "马云封面任务": Icons.person,
      "线下活动": Icons.android,
    }
  ];

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        itemCount: blocks.length,
        itemBuilder: (context, blocksIndex) {
          return Container(
            //height: 200,
            margin: const EdgeInsets.symmetric(vertical: 10.0),
            decoration: BoxDecoration(
              border: Border(
                top: BorderSide(
                  width: 1.0,
                  color: Color(0xffaaaaaa),
                ),
                bottom: BorderSide(
                  width: 1.0,
                  color: Color(0xffaaaaaa),
                ),
              ),
            ),
            child: ListView.separated(
                //滑动冲突
                physics: NeverScrollableScrollPhysics(),
                shrinkWrap: true,
                itemBuilder: (context, mapIndex) {
                  return InkWell(
                    onTap: () {
                      //TODO
                      _handleItemClick(
                          blocks[blocksIndex].keys.elementAt(mapIndex));
                    },
                    child: Container(
                      height: 60.0,
                      child: ListTile(
                        leading: Icon(
                            blocks[blocksIndex].values.elementAt(mapIndex)),
                        title:
                            Text(blocks[blocksIndex].keys.elementAt(mapIndex)),
                        trailing: Icon(Icons.arrow_forward_ios),
                      ),
                    ),
                  );
                },
                separatorBuilder: (context, index) {
                  return Divider(
                    height: 2.0,
                    color: Color(0xffff0000),
                  );
                },
                itemCount:
                    blocks[blocksIndex].length), //color: Color(0xffff0000),
          );
        });
  }

  void _handleItemClick(String key) {
    switch (key) {
      case "开源众包":
        _navWebPage(key, "https://www.oschina.net/sharing-plan");
        break;
      case "开源软件":
        //待补充
        _navWebPage(key, "https://www.oschina.net/sharing-plan");
        break;
      case "码云推荐":
        _navWebPage(key, "https://www.oschina.net/sharing-plan");
        break;
      case "代码片段":
        _navWebPage(key, "https://www.oschina.net/sharing-plan");
        break;
      case "扫一扫":
        _scan();

        break;
      case "摇一摇":
        Navigator.of(context).push(MaterialPageRoute(builder: (context) {
          return ShakePage();
        }));
        break;
      case "马云封面任务":
        break;
      case "线下活动":
        break;
    }
  }

  Future _scan() async {
    ScanResult result = await BarcodeScanner.scan();
    print("_scan result = $result");
    print("_scan result = ${result.formatNote}");
    print("_scan result = ${result.type}");
    print("_scan result = ${result.format}");
    print("_scan result = ${result.rawContent}");

    // I/flutter (27934): _scan result = Instance of 'ScanResult'
    // I/flutter (27934): _scan result =
    // I/flutter (27934): _scan result = Barcode
    // I/flutter (27934): _scan result = qr
    // I/flutter (27934): _scan result = aaaa
  }

  void _navWebPage(String key, String url) {
    if (key == null && url == null) {
      return;
    }
    Navigator.of(context).push(MaterialPageRoute(builder: (context) {
      return CommonWebPage(
        title: key,
        url: url,
      );
    }));
  }
}
